package com.hikeen.launcherx.ui;

import static com.dou361.dialogui.DialogUIUtils.showToast;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import com.hikeen.launcherx.FragmentChangeAdapter;
import com.hikeen.launcherx.OneFramgent;
import com.hikeen.launcherx.ThreeFramgent;
import com.hikeen.launcherx.TwoFramgent;
import com.hikeen.launcherx.ViewPagerIndicator;
import com.hikeen.launcherx.bean.AppInfoss;
import com.hikeen.launcherx.floatwindow.EventMessage;
import com.hikeen.launcherx.receiver.AppJTReceiver;
import com.hikeen.launcherx.utils.AnimationUtil;
import com.hikeen.launcherx.utils.FileImageUtils;
import com.hikeen.launcherx.utils.FloatingDraftButton;
import com.hikeen.launcherx.utils.GsonUtils;
import com.hikeen.launcherx.utils.SPUtils;
import com.hjq.language.MultiLanguages;
import com.hikeen.launcherx.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private static String TAG = "MAIN-LAUNCHER";

    private FragmentChangeAdapter fadapter;
    private List<Fragment> flist;//声明一个存放Fragment的List
    private ViewPager viewpager;
    private ViewPagerIndicator viewPagerIndicator;
    private ImageView ivLeft;
    private ImageView ivRight;
    private Intent intent;
    private LinearLayout ll_bottom_tab1;
    private LinearLayout ll_bottom_tab2;
    private LinearLayout ll_bottom_tab3;
    private LinearLayout ll_bottom_tab4;
    private LinearLayout ll_bottom_tab5;
    private LinearLayout ll_bottom_tab6;
    private LinearLayout ll_bottom_tab7;
    private Intent serviceintent1;
    private Intent serviceintent2;


    FloatingDraftButton floatingDraftButton;

    FloatingActionButton liveness;

    FloatingActionButton floatingActionButton2;

    FloatingActionButton floatingActionButton3;

    FloatingActionButton floatingActionButton4;
    private AppJTReceiver mMyInstallReceiver;
    public static ThreeFramgent threeFramgent;
    public static TwoFramgent twoFramgent;
    public static OneFramgent oneFramgent;

    public static int postionll = -1;
    // 是否需要重启
    public boolean restart = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: main-oncreate");
        super.onCreate(savedInstanceState);
        if(!this.isTaskRoot()) { //判断该Activity是不是任务空间的源Activity，“非”也就是说是被系统重新实例化出来
            //如果你就放在launcher Activity中话，这里可以直接return了
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;//finish()之后该活动会继续执行后面的代码，你可以logCat验证，加return避免可能的exception
            }
        }
        setContentView(R.layout.activity_main);
//        openServices();
        initView();
        initPager();
        ButterKnife.bind(this);

        //动态注册监听安装卸载广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        intentFilter.addDataScheme("package");
        mMyInstallReceiver = new AppJTReceiver();
        registerReceiver(mMyInstallReceiver, intentFilter);

    }


    private void initView() {
//        boolean b = MultiLanguages.clearAppLanguage(this);
//
//        XXPermissions.with(this)
//                // 申请单个权限
////                .permission(Permission.RECORD_AUDIO)
//                // 申请多个权限
//                //.permission(Permission.Group.CALENDAR)
//                // 申请安装包权限
//                //.permission(Permission.REQUEST_INSTALL_PACKAGES)
//                // 申请悬浮窗权限
//                .permission(Permission.SYSTEM_ALERT_WINDOW)
//                .permission(Permission.WRITE_EXTERNAL_STORAGE)
//                .permission(Permission.READ_EXTERNAL_STORAGE)
//
//                // 申请通知栏权限
//                //.permission(Permission.NOTIFICATION_SERVICE)
//                // 申请系统设置权限
//                //.permission(Permission.WRITE_SETTINGS)
//                // 设置权限请求拦截器
//                //.interceptor(new PermissionInterceptor())
//                // 设置不触发错误检测机制
//                //.unchecked()
//                .request(new OnPermissionCallback() {
//
//                    @Override
//                    public void onGranted(List<String> permissions, boolean all) {
//                        if (all) {
////                            toast("获取录音和日历权限成功");
//                            //  ToastUtils.show("获取成功");
//                        } else {
//                            // ToastUtils.show("获取部分权限成功，但部分权限未正常授予");
//                        }
//                    }
//
//                    @Override
//                    public void onDenied(List<String> permissions, boolean never) {
//                        if (never) {
////                            toast("被永久拒绝授权，请手动授予录音和日历权限");
//                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(MainActivity.this, permissions);
//                        } else {
////                            toast("获取录音和日历权限失败");
//                        }
//                    }
//                });


        floatingDraftButton = findViewById(R.id.floatingActionButton);
        liveness = findViewById(R.id.floatingActionButton_liveness);

        floatingActionButton2 = findViewById(R.id.floatingActionButton_2);

        floatingActionButton3 = findViewById(R.id.floatingActionButton_3);

        floatingActionButton4 = findViewById(R.id.floatingActionButton_4);

//         floatingActionButton5 = findViewById(R.id.floatingActionButton_5);

        floatingDraftButton.registerButton(liveness);
        floatingDraftButton.registerButton(floatingActionButton2);
        floatingDraftButton.registerButton(floatingActionButton3);
        floatingDraftButton.registerButton(floatingActionButton4);
//        floatingDraftButton.registerButton(floatingActionButton5);

        floatingDraftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                //弹出动态Button
                AnimationUtil.slideButtons(MainActivity.this, floatingDraftButton);
            }
        });
        //标注
        liveness.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭动态Button
                AnimationUtil.slideButtons(MainActivity.this, floatingDraftButton);
                Intent mIntent = new Intent();
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName component = new ComponentName("com.hikeen.mark", "com.hikeen.mark.MainActivity");
                mIntent.setComponent(component);
                startActivity(mIntent);
            }
        });
        //截屏
        floatingActionButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭动态Button
                AnimationUtil.slideButtons(MainActivity.this, floatingDraftButton);
                Intent serviceIntent = new Intent();
                ComponentName components = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.WindowService");
                serviceIntent.setComponent(components);
                serviceIntent.putExtra("key", "screenapp_start");
                startService(serviceIntent);
            }
        });
        //返回
        floatingActionButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭动态Button
                AnimationUtil.slideButtons(MainActivity.this, floatingDraftButton);
                if (viewpager != null) {

                    viewpager.setCurrentItem(1);
                }
            }
        });
        //主页
        floatingActionButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭动态Button
                AnimationUtil.slideButtons(MainActivity.this, floatingDraftButton);
                if (viewpager != null) {

                    viewpager.setCurrentItem(1);
                }
            }
        });


        viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewPagerIndicator = findViewById(R.id.view_page_indicator);

        ivLeft = findViewById(R.id.iv_left);
        ivRight = findViewById(R.id.iv_right);

        ll_bottom_tab1 = findViewById(R.id.ll_bottom_tab1);
        ll_bottom_tab2 = findViewById(R.id.ll_bottom_tab2);
        ll_bottom_tab3 = findViewById(R.id.ll_bottom_tab3);
        ll_bottom_tab4 = findViewById(R.id.ll_bottom_tab4);
        ll_bottom_tab5 = findViewById(R.id.ll_bottom_tab5);
        ll_bottom_tab6 = findViewById(R.id.ll_bottom_tab6);
        ll_bottom_tab7 = findViewById(R.id.ll_bottom_tab7);

        ll_bottom_tab1.setOnClickListener(this);
        ll_bottom_tab2.setOnClickListener(this);
        ll_bottom_tab3.setOnClickListener(this);
        ll_bottom_tab4.setOnClickListener(this);
        ll_bottom_tab5.setOnClickListener(this);
        ll_bottom_tab6.setOnClickListener(this);
        ll_bottom_tab7.setOnClickListener(this);

    }

    private void initPager() {
        flist = new ArrayList<>();//这里如果不写的话,就没有存放Fragment的list了
        //是会报错的
        //添加Fragment
        threeFramgent = new ThreeFramgent();
        twoFramgent = new TwoFramgent();
        oneFramgent = new OneFramgent();
        flist.add(oneFramgent);
        flist.add(twoFramgent);
        flist.add(threeFramgent);
        fadapter = new FragmentChangeAdapter(getSupportFragmentManager(), flist);
        viewpager.setAdapter(fadapter);
        viewPagerIndicator.setCirclerCount(flist.size());
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //positionOffset 表示距离 position的偏移量有多少
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                viewPagerIndicator.setPositionOffset(position, positionOffset);
                if (positionOffset == 0) {
                    viewPagerIndicator.setSelectPosition(position);
                }
            }

            @Override
            public void onPageSelected(int position) {
                //TextPaint textPaint = tvNumberStyle.getPaint();
                //float len = textPaint.measureText(tvNumberStyle.getText().toString().trim());
                //LogUtils.e(TAG , "tvNumberStyle length : " + tvNumberStyle.getMeasuredWidth());
                if (position == 0) {
//                    EventMessage msg = new EventMessage(8,"addlist");
//                    EventBus.getDefault().post(msg);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        if (!serviceIsRunning() || !serviceIsRunning1()) {
//
//            if (!serviceIsRunning()) {
//        if(!MyAccessibilityService.flag){
//                Intent intent = new Intent(this, MyAccessibilityService.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//
//                if (viewpager != null) {
//
//                    viewpager.setCurrentItem(1);
//                }
//
//
//            if (!serviceIsRunning1()) {
//                Intent intent2 = new Intent(this, MyRightService.class);
//                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                bindService(intent2, serviceConnection2, Context.BIND_AUTO_CREATE);
//
//                if (viewpager != null) {
//
//                    viewpager.setCurrentItem(1);
//                }
//            }
////            serviceintent1 = new Intent(this, MyAccessibilityService.class);
////            startService(serviceintent1);
//            //绑定目标Service
////            Intent intent = new Intent(this, MyAccessibilityService.class);
////            bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//
//
////            serviceintent2 = new Intent(this, MyRightService.class);
////            startService(serviceintent2);
//
////            if(viewpager!=null){
////
////                viewpager.setCurrentItem(1);
////            }
//
//        } else {
//
//            postionll = getIntent().getIntExtra("postion", -1);
//            if (postionll >= 0) {
//                if (viewpager != null) {
//
//                    viewpager.setCurrentItem(postionll);
//                }
//
//            } else {
//                if (viewpager != null) {
//
//                    viewpager.setCurrentItem(1);
//                }
//            }
//
//
//        }


//        MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
//            @Override
//            public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
//                Log.e("restarteeee", "onResume: "+oldLocale+"--------"+ newLocale);
//            }
//
//            @Override
//            public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
//                Log.e("restarteeeesssss", "onResume: "+oldLocale+"--------"+ newLocale);
//                // 跟随系统语种（返回 true 表示需要重启 App）
//                ToastUtils.show("首页监听---------------------");
////                runOnUiThread(new Runnable() {
////                    @Override
////                    public void run() {
////                        MyAccessibilityService.setyuyan(MainActivity.this);
////                    }
////                });
//
////                AppUtils.restartAPP(MainActivity.this);
//
////             android.os.Process.killProcess(android.os.Process.myPid());//结束PId的进程，并释放资源
////                System.exit(0);
//
//            }
//        });
        viewpager.setCurrentItem(0);
        viewpager.setOffscreenPageLimit(3);//防止viewpager加载fragment不出的数据
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
        //活动onDestroy方法取消注册
        unregisterReceiver(mMyInstallReceiver);
        Log.v("tv", " onDestroy 1111111111111111111111111");
//        stopService(serviceintent1);
//        stopService(serviceintent2);
        unbindService(serviceConnection);// 解除绑定，断开连接
        unbindService(serviceConnection2);// 解除绑定，断开连接

    }

    // 在Activity中，我们通过ServiceConnection接口来取得建立连接与连接意外丢失的回调
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
// 建立连接
// 获取服务的操作对象
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
// 连接断开
        }
    };
    ServiceConnection serviceConnection2 = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
// 建立连接
// 获取服务的操作对象
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
// 连接断开
        }
    };


    /**
     * 判断自己的应用的AccessibilityService是否在运行
     *
     * @return
     */
    private boolean serviceIsRunning() {
        ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Short.MAX_VALUE);
        for (ActivityManager.RunningServiceInfo info : services) {
            if (info.service.getClassName().equals(getPackageName() + ".floatwindow.MyAccessibilityService")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断自己的应用的AccessibilityService是否在运行
     *
     * @return
     */
    private boolean serviceIsRunning1() {
        ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Short.MAX_VALUE);
        for (ActivityManager.RunningServiceInfo info : services) {
            if (info.service.getClassName().equals(getPackageName() + ".floatwindow.MyRightService")) {
                return true;
            }
        }
        return false;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventMessage event) {
        // Do something
        if (event.getType() == 9) {
            if (event.getMessage().equals("setitem0")) {
                if (viewpager != null) {

                    viewpager.setCurrentItem(0);
                }
            }


        }
//        if (event.getType()==10) {
//            if (event.getMessage().equals("addapp")) {
//                EventMessage msg = new EventMessage(11,"refreshlist");
//                msg.setMessage2("");
//                EventBus.getDefault().post(msg);
//
//            } else if (event.getMessage().equals("remaveapp")) {
////               new Thread(new Runnable() {
////                   @Override
////                   public void run() {
////                       try {
////                           Thread.sleep(3000);
//
//                           EventMessage msg = new EventMessage(11,"refreshlist");
//                           msg.setMessage2(event.getMessage2());
//                           EventBus.getDefault().post(msg);
//                Log.e("logeeeeee", "onMessageEvent: " );
////                       } catch (InterruptedException e) {
////                           e.printStackTrace();
////                       }
////                   }
////               });
////                Intent intent = new Intent("jerry");
////                intent.putExtra("change", event.getMessage2());
////                LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
//
////                threeFramgent.chushihua(event.getMessage2());
//
//
//                EventMessage msg2 = new EventMessage(12,"refreshlist");
//                msg2.setMessage2(event.getMessage2());
//                EventBus.getDefault().post(msg2);
//
//
//            }
//        }

    }

    @Override
    public void onStart() {
        Log.d(TAG, "onStart: ");
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_bottom_tab1:
//                serviceIntent = new Intent();
//                ComponentName component = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.WindowService");
//                serviceIntent.setComponent(component);
//                serviceIntent.putExtra("key","signalview_start");
//                startService(serviceIntent);

                MyAccessibilityService.openwinds();


                break;
            case R.id.ll_bottom_tab2:
//                intent = new Intent();
//                ComponentName cn2 = new ComponentName("com.jiuhong.mytvapplication", "com.jiuhong.mytvapplication.ui.MainActivity");
//                intent.setComponent(cn2);
//                intent.putExtra("postion", 1);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
//                initView();
//                initPager();
                if (viewpager != null) {

                    viewpager.setCurrentItem(1);
                }

                break;
            case R.id.ll_bottom_tab3:
                if (viewpager != null) {
                    viewpager.setCurrentItem(0);
                }
                break;
            case R.id.ll_bottom_tab4:
//                intent = new Intent();
//                ComponentName cn1 = new ComponentName("com.jiuhong.mytvapplication", "com.jiuhong.mytvapplication.ui.MainActivity");
//                intent.setComponent(cn1);
//                intent.putExtra("postion", 2);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
//                initView();
//                initPager();
                if (viewpager != null) {
                    viewpager.setCurrentItem(2);
                }
                break;
            case R.id.ll_bottom_tab5:

                MainActivity.isShowOne = true;
                intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://www.baidu.com");
                intent.setData(content_url);
                intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.Main");
                startActivity(intent);
//                openUrl("http://www.baidu.com");
//                 intent = new Intent();
//                intent.setAction(Intent.ACTION_VIEW);
//                Uri content_url = Uri.parse("http://www.baidu.com");
//                intent.setData(content_url);
//                intent.setClassName("com.android.chrome","com.google.android.apps.chrome.ChromeTabbedActivity");
////                intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
//                startActivity(intent);


//                Intent intent=new Intent(Intent.ACTION_VIEW);
//
//                intent.setData(Uri.parse("http://www.baidu.com"));
//
//                startActivity(intent);


                saveAppClick("com.android.chrome");
                break;
            case R.id.ll_bottom_tab6:
                MainActivity.isShowOne = true;

                intent = getPackageManager().getLaunchIntentForPackage("com.android.setting3399");
                startActivity(intent);

                saveAppClick("com.android.setting3399");
                break;
            case R.id.ll_bottom_tab7:

                Intent intent = new Intent();
                ComponentName components = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.common.CallService");
                intent.setComponent(components);
                intent.putExtra("key", "lockapp_start");
                startService(intent);
                break;

        }
    }

    public static boolean isShowOne = false;

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume: ");
        super.onResume();
//        initView();
//        initPager();
        isShowOne = false;
//        if (viewpager.getCurrentItem() == 0) {
//            EventMessage msg = new EventMessage(8, "addlist");
//            EventBus.getDefault().post(msg);
////            ToastUtils.show("onResume 执行了");
//        } else if (viewpager.getCurrentItem() == 1 && (postionll < 0 || postionll == 1)) {
//            EventMessage msg = new EventMessage(8, "addlist");
//            EventBus.getDefault().post(msg);
//        }


    }

    public void openUrl(String url) {
        String[] browser = {"com.tencent.mtt", "com.UCMobile", "com.uc.browser", "com.oupeng.browser", "com.oupeng.mini.android", "com.android.browser", "com.android.chrome"};

        Intent intent = null;
        for (String br : browser) {

            String clsName = null;
            try {
                PackageManager pm = getApplicationContext().getPackageManager();
                Intent intent1 = pm.getLaunchIntentForPackage(br);
                ComponentName act = intent1.resolveActivity(pm);
                clsName = act.getClassName();
                showToast(clsName);
                Log.e("className", "clsName = " + clsName);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e("className", "clsName11 = " + e);
            }
            if (clsName == null) {
                break;
            }
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            intent.setClassName(br, clsName);
            break;
        }
        if (intent == null) {
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
        }
        startActivity(intent);
    }


    private void saveAppClick(String pack) {
        //这里通过包名来获取app名
        String name = "";
        PackageInfo packageInfo = null;
        PackageManager packageManager = getApplicationContext().getPackageManager();
        try {
            packageInfo = packageManager.getPackageInfo(pack, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        name = packageInfo.applicationInfo.loadLabel(packageManager).toString();
        String finalName = name;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000); // 休眠1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String s = FileImageUtils.savefilePath(MainActivity.this, finalName);
                File filePic = new File(s);
                filePic.getParentFile().mkdirs();
                screenShotByShell(s);
            }
        }).start();
        //保存当前打开的APP  到第一条
        String savelist = SPUtils.getString("savelist", "");
        List<AppInfoss> saveL = new ArrayList<>();
        if (TextUtils.isEmpty(savelist)) {
            AppInfoss appInfoss = new AppInfoss();
            appInfoss.setAppName(name);
            appInfoss.setPackageName(pack);
            saveL.add(appInfoss);
        } else {
            List<AppInfoss> appInfosses = GsonUtils.getPersons(savelist, AppInfoss.class);
            int index = -1;
            for (int i = 0; i < appInfosses.size(); i++) {
                if (name.equals(appInfosses.get(i).getAppName()) &&
                        pack.equals(appInfosses.get(i).getPackageName())) {
                    index = i;
                }
            }
            if (index >= 0) {
                appInfosses.remove(index);
            }
            AppInfoss appInfoss = new AppInfoss();
            appInfoss.setAppName(name);
            appInfoss.setPackageName(pack);
            if (appInfosses.size() > 7) {
                appInfosses.remove(appInfosses.size() - 1);
                appInfosses.add(0, appInfoss);
            } else {
                appInfosses.add(0, appInfoss);
            }
            saveL.addAll(appInfosses);
        }
        String s = new Gson().toJson(saveL);
        Log.e("appinfo", "onClick: " + s);
        SPUtils.putString("savelist", s);
    }

    //    三、使用screencap的adb命令
//
//    命令行是这样的：adb shell screencap -p file_path
//
//    在代码中执行，就不用adb shell，直接screencap -p file_path，调用Runtime的进程来执行：

    public void screenShotByShell(String filePath) {
        String shotCmd = "screencap -p " + filePath + " \n";
        try {
            Runtime.getRuntime().exec(shotCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        // 绑定语种
        super.attachBaseContext(MultiLanguages.attach(newBase));
    }


}




